
/**
 *	TrueMusic - simple java mp3 player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *	
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the 
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *	
 *	Copyright (c)2005-2007 Ryan Macnak <rmacnak@gmail.com>
 */

package com.servequake.truemusic;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class TagDialog extends JDialog implements ActionListener
{
	Audio a;

	JTextField title,artist,album,track;
	JTextArea lyrics = new JTextArea();
	JButton save = new JButton("Save");
	JButton close = new JButton("Close");

	TagDialog(JFrame parent, Audio a)
	{
		super(parent, "Edit Tag for "+a.getResource(), true );
		this.a = a;

		try{ if(a instanceof Mp3Audio) ((Mp3Audio)a).readTag(); }catch(Exception e){}

		title = new JTextField(this.a.getTitle());
		artist = new JTextField(this.a.getArtist());
		album = new JTextField(this.a.getAlbum());
		track = new JTextField(this.a.getTrack());
		lyrics.setText(this.a.getLyrics());

		save.addActionListener(this);
		close.addActionListener(this);

		if( (!(a instanceof Mp3Audio)) || (!new java.io.File(a.getResource()).canWrite()) )
		//TODO: if( (!(a instanceof TaggableAudio)) || (!new java.io.File(a.getResource()).canWrite()) )
		{
			title.setEnabled(false);
			artist.setEnabled(false);
			album.setEnabled(false);
			track.setEnabled(false);
			lyrics.setEnabled(false);
			save.setEnabled(false);
		}

		JPanel top = new JPanel( new GridLayout(5,2) );
		top.add( new JLabel("Title:", SwingConstants.CENTER) );
		top.add( title);
		top.add( new JLabel("Artist:", SwingConstants.CENTER) );
		top.add( artist);
		top.add( new JLabel("Album:", SwingConstants.CENTER) );
		top.add( album);
		top.add( new JLabel("Track #:", SwingConstants.CENTER) );
		top.add( track);
		top.add( new JLabel("Lyrics:", SwingConstants.CENTER) );
		top.add( new JLabel("Format: "+a.formatName(), SwingConstants.CENTER) );

		JPanel bottom = new JPanel();
		bottom.add(save);
		bottom.add(close);

		setLayout(new BorderLayout());
		add(top, BorderLayout.NORTH);
		add(new JScrollPane(lyrics));
		add(bottom, BorderLayout.SOUTH);
	
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(400,400);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==save)
		{
			Mp3Audio a = (Mp3Audio) this.a;
			try
			{
				a.writeTag( title.getText(), artist.getText(), album.getText(), track.getText(), lyrics.getText() );
			}
			catch(Exception ex)
			{
				JOptionPane.showMessageDialog(this, "Error writing tag for file\n"+a.getResource()+"\n"+ex.toString(), "Error writing tag!", JOptionPane.ERROR_MESSAGE);
			}
		}
		if(e.getSource()==close)
		{
			dispose();
		}
	}
}
